package Controller;

import DAO.FoundDao;
import DAO.LostDao;
import com.alibaba.fastjson.JSON;
import entity.Found;
import entity.Lost;
import entity.Return;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "lostAction", value = "/lostAction")
public class lostController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        super.doGet(request, response);
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        super.doPost(request, response);
        String method = request.getParameter("method");
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        boolean ISadmin = request.getSession().getAttribute("ISadmin").equals("true");

        if (method.equals("classify")) {
            String classify = request.getParameter("classify");
            ArrayList<Lost> lostArrayList = new LostDao().queryByClassify(classify);
            String alllostArrayList = JSON.toJSONString(lostArrayList);
            Return r = Return.ok().data("data",alllostArrayList);
            String returnStr = JSON.toJSONString(r);
            response.getWriter().println(returnStr);

        } else if (method.equals("mine")) {
            String search = request.getParameter("userid");
            ArrayList<Lost> lostArrayList = new LostDao().queryByID(search);
            String alllostArrayList = JSON.toJSONString(lostArrayList);

            Return r = Return.ok().data("data",alllostArrayList);
            String returnStr = JSON.toJSONString(r);
            response.getWriter().println(returnStr);

        } else if (method.equals("all")) {
            ArrayList<Lost> lostArrayList = new LostDao().queryAll();
            String alllostArrayList = JSON.toJSONString(lostArrayList);
            Return r = Return.ok().data("data",alllostArrayList);
            String returnStr = JSON.toJSONString(r);
            response.getWriter().println(returnStr);

        } else if (method.equals("add")) {
            String lost_name = request.getParameter("lost_name");
            String lost_time = request.getParameter("lost_time");
            String describe = request.getParameter("describe");
            String place = request.getParameter("place");
            String sort = request.getParameter("sort");
            String userID = request.getParameter("userID");
            Lost lost = new LostDao().queryAddLost(lost_name,lost_time,describe,place,sort,userID);
            String alllost = JSON.toJSONString(lost);
            Return r = Return.ok().data("data",alllost);
            String returnStr = JSON.toJSONString(r);
            response.getWriter().println(returnStr);
            if (ISadmin){
                request.getRequestDispatcher("jsp/addthing.jsp").forward(request, response);
            }else {
                request.getRequestDispatcher("jsp/addthinguser.jsp").forward(request, response);
            }

        }  else if (method.equals("delete")) {
            String delete = request.getParameter("delete");
            boolean flag = new LostDao().deleteLost(delete);
            Return r = null;
            if (flag) {
                r = Return.ok();
            } else {
                r = Return.error();
            }
            String returnStr = JSON.toJSONString(r);
            response.getWriter().println(returnStr);
            if (ISadmin){
                request.getRequestDispatcher("jsp/sortthing.jsp").forward(request, response);
            }else {
                request.getRequestDispatcher("jsp/sortUserqueryFound.jsp").forward(request, response);
            }

        }
    }
}
